package pl.edu.agh.iosr.gamblingzone.userpanel.action;

import static org.jboss.seam.ScopeType.SESSION;

import java.util.Date;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.BettingSlipDAOBean;
import pl.edu.agh.iosr.gamblingzone.model.BettingSlip;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;

@Stateful
@Scope(SESSION)
@Name("userSlips")
@Restrict("#{identity.loggedIn}")
public class BettingSlipsViewAction implements BettingSlipsView {

	@In
	private User user;
	
	@In(required=true)
	DAOFactory dao;

	@PersistenceContext
	private EntityManager em;

	@DataModel
	private List<BettingSlip> slips;

	@DataModelSelection(value="slips")
	@Out(required = false)
	private BettingSlip slip;
	
	@Logger
	Log log;
	
	@In
	private FacesMessages facesMessages;
	
	private BettingSlipDAO slipDAO;
	private int numberOfPages = 0;
	private int pageSize = 0;
	private int currentPage = 0;
	
	@Create
	public void init() {
		slipDAO = dao.getSlipDAO();
		
		numberOfPages = slipDAO.getPageCount(user);
		pageSize = slipDAO.getPageSize();
		currentPage = 1;
	}

	public List<BettingSlip> findBettingSlips() {
		slips = slipDAO.findAll(user, currentPage - 1);
		return null;
	}
	
	public void previous(){
		if(currentPage > 1) {
			currentPage--;			
			findBettingSlips();
		}
	}
	
	public void next(){
		if(currentPage < numberOfPages) {
			currentPage++;			
			findBettingSlips();
		}
	}
	
	public void first() {
		currentPage = 1;		
	}
	
	public void last() {
		currentPage = numberOfPages;		
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	public int getNumberOfPages() {
		return numberOfPages;
	}

	public void setNumberOfPages(int numberOfPages) {
		this.numberOfPages = numberOfPages;
	}

	@Remove
	public void destroy() {
	}
}
